//----------------------------------------------------------------
//Tên Class: Cls_SYS_LogErr
//Người thực hiện: NhatTHM
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace Library.Business {
	public class Cls_SYS_LogErr
	{
		protected string sSQL;
		//Tham số cho Procudure
		#region Tham số cho Procudure
		protected const string st_SYS_LogErr_Insert = "st_SYS_LogErr_Insert";
		protected const string st_SYS_LogErr_Update = "st_SYS_LogErr_Update";
		protected const string st_SYS_LogErr_Update_Hidden = "st_SYS_LogErr_Update_Hidden";
		protected const string st_SYS_LogErr_Delete = "st_SYS_LogErr_Delete";
		protected const string st_SYS_LogErr_SelectAll = "st_SYS_LogErr_SelectAll";
		protected const string st_SYS_LogErr_SelectAll_Key = "st_SYS_LogErr_SelectAll_Key";
		protected const string st_SYS_LogErr_SelectByID = "st_SYS_LogErr_SelectByID";
		protected const string st_SYS_LogErr_SelectByID_Stand = "st_SYS_LogErr_SelectByID_Stand";
		protected const string TABLE_NAME = "tbSYS_LogErr";
		#endregion Tham số cho Procudure
		//Các tên filed của table.
		#region Các tên filed của table.
		public string fn_Id = "Id";
		public string fn_Time_Sort = "Time_Sort";
		public string fn_NameClass = "NameClass";
		public string fn_Description = "Description";
		public string fn_Hidden = "Hidden";
		//Các filed của table.
		private int _id_find;
		private int _id;
		private DateTime _time_Sort;
		private string _nameClass;
		private string _description;
		private bool _hidden;
		#endregion Các tên filed của table.
		#region Các phương thức cho table.
		//Thuộc tính Id_find
		public int Id_find
		{
			get{return this._id_find;}
			set{this._id_find = value;}
		}
		//Thuộc tính Id
		public int Id
		{
			get{return this._id;}
			set{this._id = value;}
		}
		//Thuộc tính Time_Sort
		public DateTime Time_Sort
		{
			get{return this._time_Sort;}
			set{this._time_Sort = value;}
		}
		//Thuộc tính NameClass
		public string NameClass
		{
			get{return this._nameClass;}
			set{this._nameClass = value;}
		}
		//Thuộc tính Description
		public string Description
		{
			get{return this._description;}
			set{this._description = value;}
		}
		//Thuộc tính Hidden
		public bool Hidden
		{
			get{return this._hidden;}
			set{this._hidden = value;}
		}
		#endregion Các phương thức cho table.
		/// <summary>
		/// Hàm khởi tạo không có tham số.
		/// </summary>
		public Cls_SYS_LogErr()
		{
			try
			{
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}

		/// <summary>
		/// Hàm khởi tạo có  tham số.
		/// </summary>
		public Cls_SYS_LogErr(DateTime time_sort, string nameclass, string description, bool hidden)
		{
			this._time_Sort = time_sort;
			this._nameClass = nameclass;
			this._description = description;
			this._hidden = hidden;
		}
        /// <summary>
        /// Đếm tổng số bảng ghi.
        /// </summary>
        /// <returns></returns>
        public int countRows(bool Hidden)
        {
            SqlConnection con = new AccessDB().get_Conn();
            SqlCommand sqlComm = new SqlCommand("st_SYS_LogErr_Count", con);
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.Parameters.Add("@Hidden", SqlDbType.Bit).Value = Hidden;
            try
            {
                con.Open();
                int intResult= (int)sqlComm.ExecuteScalar();
                return intResult;
            }
            catch (SqlException ex)
            {
                new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
                throw ex;
            }
            finally
            {
                if (sqlComm != null) sqlComm.Dispose();
                if (con != null) con.Close();
            }
        }
        /// <summary>
        /// Đếm tổng số bảng ghi với điều kiện khóa chính.
        /// </summary>
        /// <returns></returns>
        public bool countRows_Key()
        {
            SqlConnection con = new AccessDB().get_Conn();
            SqlCommand sqlComm = new SqlCommand("st_SYS_LogErr_Count_Key", con);
            sqlComm.CommandType = CommandType.StoredProcedure;
            try
            {
                sqlComm.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
                con.Open();
                if (((int)sqlComm.ExecuteScalar()) == 1) return true;
                else return false;
            }
            catch (SqlException ex)
            {
                new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
                throw ex;
            }
            finally
            {
                if (sqlComm != null) sqlComm.Dispose();
                if (con != null) con.Close();
            }
        }
        /// <summary>
        /// Get max ID auto.
        /// </summary>
        /// <returns></returns>
        public int getMaxIdAuto()
        {
            SqlConnection con = new AccessDB().get_Conn();
        	sSQL = "SELECT MAX(Id) FROM tbSYS_LogErr";

            SqlCommand sqlComm = new SqlCommand(sSQL, con);
            sqlComm.CommandType = CommandType.Text;
            try
            {
                con.Open();
                return (int)sqlComm.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
                throw ex;
                return -1;
            }
            finally
            {
                if (sqlComm != null) sqlComm.Dispose();
                if (con != null) con.Close();
            }
        }
		/// <summary>
		/// Get DataTable với table class.
		/// </summary>
		/// <param name="ID">ID</param>
		/// <returns></returns>
		public DataTable getDataTable_My_Key()
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_SelectAll_Key, con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			try
			{
				SqlDataAdapter sqlDTA = new SqlDataAdapter(sqlComm);
				DataTable dt = new DataTable();
		        con.Open();
				sqlDTA.Fill(dt);
				return dt;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
		        throw ex;
				return null;
			}
		    finally
		    {
		        if (sqlComm != null) sqlComm.Dispose();
		        if (con != null) con.Close();
		    }
		}

		/// <summary>
		/// Get DataTable với Where time.
		/// </summary>
		/// <param name="strWhere">Where get ucChonNgay t</param>
		/// <returns></returns>
		public DataTable getDataTable_Time(string strWhere)
		{
			try
			{
				sSQL = "SELECT * FROM tbSYS_LogErr" + strWhere+" Order By Time_Sort DESC,Id Desc";
				return getDataTable_SQL_pro(sSQL);
			}
			catch (SqlException ex)
			{
				return null;
			}
		}

		/// <summary>
		/// Get DataTable với Where.
		/// </summary>
		/// <returns></returns>
		public DataTable getDataTable_Where(string strWhere,string FieldSort)
		{
			try
			{
			    if (FieldSort.Trim().Length == 0) FieldSort = "";
			    else FieldSort = " Order By " + FieldSort;
			    //
			    if (strWhere.Trim().Length == 0) strWhere = "";
			    else strWhere = " WHERE " + strWhere;
				sSQL = "SELECT * FROM tbSYS_LogErr" + strWhere + FieldSort ;
				return getDataTable_SQL_pro(sSQL);
			}
			catch (SqlException ex)
			{
				return null;
			}
		}

		/// <summary>
		/// Get DataTable với table class.
		/// </summary>
		/// <param name="ID">ID</param>
		/// <returns></returns>
		public DataTable getDataTable_SQL_pro(string sSQL)
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand("st_processSQL",con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			DataTable dt = new DataTable();
			try
			{
				sqlComm.Parameters.Add("@sSQL", SqlDbType.NVarChar).Value = sSQL;
				SqlDataAdapter sqlDTA = new SqlDataAdapter(sqlComm);
		        con.Open();
				sqlDTA.Fill(dt);
				return dt;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
		        throw ex;
				return null;
			}
            finally
            {
                if (dt != null) dt.Dispose();
                if (sqlComm != null) sqlComm.Dispose();
                if (con != null) con.Close();
            }
		}

		/// <summary>
		/// Get DataTable với table class.
		/// </summary>
		/// <param name="ID">ID</param>
		/// <returns></returns>
		public DataTable getDataTable_My()
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_SelectAll, con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			SqlDataAdapter sqlDTA = new SqlDataAdapter(sqlComm);
		    DataTable dt = new DataTable();
			try
			{
		        con.Open();
				sqlDTA.Fill(dt);
				return dt;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
		        throw ex;
				return null;
			}
		    finally
		    {
		        if (dt != null) dt.Dispose();
		        if (sqlComm != null) sqlComm.Dispose();
		        if (con != null) con.Close();
		    }
		}

		public DataTable getDataTable_Hidden(bool Hidden)
		{
		    SqlConnection con = new AccessDB().get_Conn();
		    SqlCommand sqlComm = new SqlCommand("st_SYS_LogErr_SelectAll_Hidden", con);
		    sqlComm.CommandType = CommandType.StoredProcedure;
		    sqlComm.Parameters.Add("@Hidden", SqlDbType.Bit).Value = Hidden;

		    SqlDataAdapter sqlDTA = new SqlDataAdapter(sqlComm);
		    DataTable dt = new DataTable();
			try
			{
		        con.Open();
				sqlDTA.Fill(dt);
				return dt;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
		        throw ex;
				return null;
			}
		    finally
		    {
		        if (sqlDTA != null) sqlDTA.Dispose();
		        if (dt != null) dt.Dispose();
		        if (sqlComm != null) sqlComm.Dispose();
		        if (con != null) con.Close();
		    }
		}

		/// <summary>
		/// Get DataTable với table class.
		/// </summary>
		/// <param name="ID">ID</param>
		/// <returns></returns>
		public DataTable getDataTable_My(int key_Id)
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_SelectByID, con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			try
			{
		        con.Open();
				//
				sqlComm.Parameters.Add("@Id", SqlDbType.Int).Value = key_Id;
				//
				SqlDataAdapter sqlDTA = new SqlDataAdapter(sqlComm);
				DataTable dt = new DataTable();
				sqlDTA.Fill(dt);
				return dt;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
		        throw ex;
			}
		    finally
		    {
		        if (sqlComm != null) sqlComm.Dispose();
		        if (con != null) con.Close();
		    }
		}

		/// <summary>
		/// Thực hiện SQL dữ liệu.
		/// </summary>
		/// <returns></returns>
		public bool doSQL(string sSQL)
		{
            SqlConnection con = new AccessDB().get_Conn();
            SqlCommand sqlComm = new SqlCommand("st_processSQL", con);
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.Parameters.Add("@sSQL", SqlDbType.NVarChar).Value = sSQL;
			try
			{
                con.Open();
				sqlComm.ExecuteNonQuery();
				return true;
			}
			catch (SqlException  ex)
			{
		        throw ex;
				return false;
			}
		    finally
		    {
		        if (sqlComm != null) con.Dispose();
		        if (con != null) con.Close();
		    }
		}
		/// <summary>
		/// Thực hiện Insert dữ liệu.
		/// </summary>
		/// <returns></returns>
		public int doInsert()
		{
        SqlConnection con = new AccessDB().get_Conn();
		SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_Insert,con);
        sqlComm.CommandType = CommandType.StoredProcedure;
        try
        {
				//
				//Time_Sort
				if (Time_Sort.Year == 1)
					sqlComm.Parameters.Add("@Time_Sort", SqlDbType.DateTime).Value = DBNull.Value;
				else
					sqlComm.Parameters.Add("@Time_Sort", SqlDbType.DateTime).Value = Time_Sort;
				//NameClass
				sqlComm.Parameters.Add("@NameClass", SqlDbType.NVarChar).Value = NameClass;
				//Description
				sqlComm.Parameters.Add("@Description", SqlDbType.NVarChar).Value = Description;
				//Hidden
				sqlComm.Parameters.Add("@Hidden", SqlDbType.Bit).Value = Hidden;
                con.Open();
				sqlComm.ExecuteNonQuery();
				return 1;
			}
			catch (SqlException  ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
				return -1;
			}
		    finally
		    {
		        if (sqlComm != null) con.Dispose();
		        if (con != null) con.Close();
		    }
		}
		/// <summary>
		/// Thực hiện update dữ liệu.
		/// </summary>
		/// <returns></returns>
		public int doUpdate()
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_Update,con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			try
			{
		        con.Open();
				//Id_find
				sqlComm.Parameters.Add("@Id_find", SqlDbType.Int).Value = Id_find;
				//Time_Sort
				if (Time_Sort.Year == 1)
					sqlComm.Parameters.Add("@Time_Sort", SqlDbType.DateTime).Value = DBNull.Value;
				else
					sqlComm.Parameters.Add("@Time_Sort", SqlDbType.DateTime).Value = Time_Sort;
				//NameClass
				sqlComm.Parameters.Add("@NameClass", SqlDbType.NVarChar).Value = NameClass;
				//Description
				sqlComm.Parameters.Add("@Description", SqlDbType.NVarChar).Value = Description;
				sqlComm.ExecuteNonQuery();
				return 1;
			}
			catch (SqlException  ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
				return -1;
			}
		    finally
		    {
		        if (sqlComm != null) con.Dispose();
		        if (con != null) con.Close();
		    }
		}
		/// <summary>
		/// Thực hiện update dữ liệu.
		/// </summary>
		/// <returns></returns>
		public int doUpdateHidden()
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_Update_Hidden, con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			try
			{
				//Id
				sqlComm.Parameters.Add("@Id", SqlDbType.Int).Value = Id_find;
				sqlComm.Parameters.Add("@Hidden", SqlDbType.Bit).Value = Hidden;
		        con.Open();
				sqlComm.ExecuteNonQuery();
				return 1;
			}
			catch (SqlException  ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
				return -1;
			}
		    finally
		    {
		        if (sqlComm != null) con.Dispose();
		        if (con != null) con.Close();
		    }
		}
		/// <summary>
		/// Thực hiện xóa dữ liệu.
		/// </summary>
		/// <returns></returns>
		public int doDelete()
		{
		    SqlConnection con = new AccessDB().get_Conn();
			SqlCommand sqlComm = new SqlCommand(st_SYS_LogErr_Delete, con);
			sqlComm.CommandType = CommandType.StoredProcedure;
			try
			{
				sqlComm.Parameters.Add("@Id", SqlDbType.Int).Value = Id_find ;
				//
		        con.Open();
		    	sqlComm.ExecuteNonQuery() ;
				return 1;
			}
			catch (SqlException ex)
			{
		        new MyLog.Log_DB().setLog(TABLE_NAME, ex.Message);
				return -1;
			}
		    finally
		    {
		        if (sqlComm != null) con.Dispose();
		        if (con != null) con.Close();
		    }
		}

	}

}
